package com.tianji.promotion.strategy.discount;

import com.tianji.common.utils.NumberUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.promotion.annotation.DiscountTypeFlag;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.enums.DiscountType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@DiscountTypeFlag(DiscountType.NO_THRESHOLD)
public class NoThresholdDiscount implements Discount {

    private static final String RULE_TEMPLATE = "无门槛抵{}元";

    /**
     * 判断当前价格是否满足优惠券使用限制
     *
     * @param totalAmount 订单总价
     * @param coupon      优惠券信息
     * @return 是否可以使用优惠券
     */
    @Override
    public boolean canUse(int totalAmount, Coupon coupon) {
        return totalAmount >= coupon.getDiscountValue();
    }

    @Override
    public int calculateDiscount(int totalAmount, Coupon coupon) {
        return coupon.getDiscountValue();
    }

    @Override
    public String getRule(Coupon coupon) {
        return StringUtils.format(RULE_TEMPLATE, NumberUtils.scaleToStr(coupon.getDiscountValue(), 2));
    }
}
